﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChessBoard : MonoBehaviour
{

    public int[,] grid { get; private set; }
    public ChessType turn { get; private set; }
    public GameObject[] chessprefabs;
    public Stack<GameObject> chessPath;



    // Start is called before the first frame update
    void Start()
    {
        chessPath = new Stack<GameObject>();
        grid = new int[15, 15];
        turn = ChessType.black;
    }

    //下棋，落子在pos[0],pos[1]处
    public void PlayChess(int[] pos)
    {
        if (grid[pos[0], pos[1]] != 0 || pos[0] < 0 || pos[0] > 14 || pos[1] < 0 || pos[1] > 14)
            return;

        //实例化棋子,并存放到栈中
        chessPath.Push(Instantiate(chessprefabs[(int)turn - 1], new Vector3(pos[0], pos[1], -1), Quaternion.identity));

        //填充grid,防止两次下在同一个地方
        grid[pos[0], pos[1]] = (int)turn;


        //检测是否结束
        if (CheckWiner(pos))
        {
            Debug.Log(turn + " win");
            Time.timeScale = 0;//停止fixedupdate执行
        }



        //下一个落子的轮次
        if (turn == ChessType.black)
        {
            turn = ChessType.white;

        }
        else
        {
            turn = ChessType.black;
        }
    }

    //检测
    bool CheckWiner(int[] pos)
    {
        if (CheckOneLineWin(pos, new int[] { 1, 0 })) return true;//横
        if (CheckOneLineWin(pos, new int[] { 0, 1 })) return true;//竖
        if (CheckOneLineWin(pos, new int[] { 1, -1 })) return true;//反斜
        if (CheckOneLineWin(pos, new int[] { 1, 1 })) return true;//正斜
        return false;
    }

    //检测某方向连成五子
    bool CheckOneLineWin(int[] pos, int[] offset)
    {
        int sum = 1;//有几个相同的棋子
        for (int x = pos[0] + offset[0], y = pos[1] + offset[1]; x < 15 && x >= 0 && y < 15 && y >= 0; x += offset[0], y += offset[1])
        {
            if (grid[x, y] == (int)turn)//棋子颜色相等(此时tur未改变)
            {
                sum++;
            }
            else//不相等了
            {
                break;
            }
        }
        for (int x = pos[0] - offset[0], y = pos[1] - offset[1]; x < 15 && x >= 0 && y < 15 && y >= 0; x -= offset[0], y -= offset[1])
        {
            if (grid[x, y] == (int)turn)//棋子颜色相等(此时tur未改变)
            {
                sum++;
            }
            else//不相等了
            {
                break;
            }
        }
        if (sum >= 5)
            return true;

        return false;
    }


    //悔棋
    public void Undo()
    {
        if (chessPath.Count > 0)
        {
            GameObject temp = chessPath.Pop();
            grid[(int)(temp.transform.position.x), (int)(temp.transform.position.y)] = 0;
            Destroy(temp);
        }
        if (chessPath.Count > 0)
        {
            GameObject temp = chessPath.Pop();
            grid[(int)(temp.transform.position.x), (int)(temp.transform.position.y)] = 0;
            Destroy(temp);
        }

    }


    // Update is called once per frame
    void Update()
    {

    }
}

public enum ChessType
{
    LookOn,
    black,
    white
}

